package com.wuxianedu.sqliteopenhelperdemo;

import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wuxianedu.sqliteopenhelperdemo.db.DBHelper;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText age;
    private EditText love;
    private Button commit;
    private Person person;
    private Button look;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
        initListener();

    }

    private void initListener() {
        commit.setOnClickListener(this);
        look.setOnClickListener(this);
    }

    private void initData() {
        person = new Person();
        String nameStr = name.getText().toString();
        if (TextUtils.isEmpty(nameStr)) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        }
        person.setName(nameStr);
        person.setAge(age.getText().toString());
        person.setLove(love.getText().toString());
    }

    private void init() {
        name = (EditText) findViewById(R.id.name_et);
        age = (EditText) findViewById(R.id.age_et);
        love = (EditText) findViewById(R.id.love_et);
        commit = (Button) findViewById(R.id.button_commit);
        look = (Button) findViewById(R.id.button_look);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_commit:
                initData();
                DBHelper dbHelper = new DBHelper(MainActivity.this);

                ContentValues values = new ContentValues();
                if (person != null) {
                    values.put("name", person.getName());
                    values.put("age", person.getAge());
                    values.put("love", person.getLove());
                }
                dbHelper.insertData(values);

                Intent intent = new Intent(MainActivity.this, ListActivity.class);
                startActivity(intent);
                break;

            case R.id.button_look:
                Intent intent2 = new Intent(MainActivity.this, ListActivity.class);
                startActivity(intent2);
                break;
        }
    }
}

